package com.djf.djfcs.controller;

import com.djf.djfcs.service.DataGeneratorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据生成器控制器
 * 提供数据生成相关的REST API接口
 * 
 * @author djf
 * @since 2025-01-01
 */
@Slf4j
@RestController
@RequestMapping("/api/data-generator")
public class DataGeneratorController {

    @Autowired
    private DataGeneratorService dataGeneratorService;

    /**
     * 批量生成完整数据（用户+订单+配送）
     * 
     * @param userCount 用户数量，默认100
     * @param orderCount 订单数量，默认1000
     * @param startMonth 开始月份，格式：2025-01，默认2025-01
     * @param endMonth 结束月份，格式：2025-12，默认2025-12
     * @return 统一响应格式
     */
    @PostMapping("/generate-complete")
    public Map<String, Object> generateCompleteData(
            @RequestParam(value = "userCount", required = false) Integer userCount,
            @RequestParam(value = "orderCount", required = false) Integer orderCount,
            @RequestParam(value = "startMonth", required = false) String startMonth,
            @RequestParam(value = "endMonth", required = false) String endMonth) {
        
        log.info("接收到批量生成数据请求 - 用户数量: {}, 订单数量: {}, 开始月份: {}, 结束月份: {}", 
                userCount, orderCount, startMonth, endMonth);
        
        String result = dataGeneratorService.generateCompleteData(userCount, orderCount, startMonth, endMonth);
        
        Map<String, Object> response = new HashMap<>();
        if (result.contains("成功") || result.contains("完成")) {
            response.put("success", true);
            response.put("message", result);
            response.put("code", 200);
        } else {
            response.put("success", false);
            response.put("message", result);
            response.put("code", 500);
        }
        response.put("timestamp", System.currentTimeMillis());
        
        return response;
    }

    /**
     * 生成用户数据
     * 
     * @param count 用户数量，默认100
     * @return 统一响应格式
     */
    @PostMapping("/generate-users")
    public Map<String, Object> generateUsers(
            @RequestParam(value = "count", required = false) Integer count) {
        
        log.info("接收到生成用户数据请求 - 数量: {}", count);
        
        String result = dataGeneratorService.generateUsers(count);
        
        Map<String, Object> response = new HashMap<>();
        if (result.contains("成功")) {
            response.put("success", true);
            response.put("message", result);
            response.put("code", 200);
        } else {
            response.put("success", false);
            response.put("message", result);
            response.put("code", 500);
        }
        response.put("timestamp", System.currentTimeMillis());
        
        return response;
    }

    /**
     * 生成订单数据
     * 
     * @param count 订单数量，默认1000
     * @param startMonth 开始月份，格式：2025-01，默认2025-01
     * @param endMonth 结束月份，格式：2025-12，默认2025-12
     * @return 统一响应格式
     */
    @PostMapping("/generate-orders")
    public Map<String, Object> generateOrders(
            @RequestParam(value = "count", required = false) Integer count,
            @RequestParam(value = "startMonth", required = false) String startMonth,
            @RequestParam(value = "endMonth", required = false) String endMonth) {
        
        log.info("接收到生成订单数据请求 - 数量: {}, 开始月份: {}, 结束月份: {}", count, startMonth, endMonth);
        
        String result = dataGeneratorService.generateOrders(count, startMonth, endMonth);
        
        Map<String, Object> response = new HashMap<>();
        if (result.contains("成功")) {
            response.put("success", true);
            response.put("message", result);
            response.put("code", 200);
        } else {
            response.put("success", false);
            response.put("message", result);
            response.put("code", 500);
        }
        response.put("timestamp", System.currentTimeMillis());
        
        return response;
    }

    /**
     * 生成配送数据
     * 
     * @param startMonth 开始月份，格式：2025-01，默认2025-01
     * @param endMonth 结束月份，格式：2025-12，默认2025-12
     * @return 统一响应格式
     */
    @PostMapping("/generate-deliveries")
    public Map<String, Object> generateDeliveries(
            @RequestParam(value = "startMonth", required = false) String startMonth,
            @RequestParam(value = "endMonth", required = false) String endMonth) {
        
        log.info("接收到生成配送数据请求 - 开始月份: {}, 结束月份: {}", startMonth, endMonth);
        
        String result = dataGeneratorService.generateDeliveries(startMonth, endMonth);
        
        Map<String, Object> response = new HashMap<>();
        if (result.contains("成功")) {
            response.put("success", true);
            response.put("message", result);
            response.put("code", 200);
        } else {
            response.put("success", false);
            response.put("message", result);
            response.put("code", 500);
        }
        response.put("timestamp", System.currentTimeMillis());
        
        return response;
    }

    /**
     * 清空所有数据
     * 
     * @return 统一响应格式
     */
    @DeleteMapping("/clear-all")
    public Map<String, Object> clearAllData() {
        
        log.info("接收到清空所有数据请求");
        
        String result = dataGeneratorService.clearAllData();
        
        Map<String, Object> response = new HashMap<>();
        if (result.contains("完成")) {
            response.put("success", true);
            response.put("message", result);
            response.put("code", 200);
        } else {
            response.put("success", false);
            response.put("message", result);
            response.put("code", 500);
        }
        response.put("timestamp", System.currentTimeMillis());
        
        return response;
    }

    /**
     * 获取服务状态
     * 
     * @return 统一响应格式
     */
    @GetMapping("/status")
    public Map<String, Object> getServiceStatus() {
        
        log.info("接收到获取服务状态请求");
        
        String result = dataGeneratorService.getServiceStatus();
        
        Map<String, Object> response = new HashMap<>();
        if (result.contains("正常")) {
            response.put("success", true);
            response.put("message", result);
            response.put("code", 200);
        } else {
            response.put("success", false);
            response.put("message", result);
            response.put("code", 500);
        }
        response.put("timestamp", System.currentTimeMillis());
        
        return response;
    }

    /**
     * 健康检查接口
     * 
     * @return 健康状态
     */
    @GetMapping("/health")
    public Map<String, Object> healthCheck() {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "数据生成器服务运行正常");
        response.put("code", 200);
        response.put("timestamp", System.currentTimeMillis());
        response.put("version", "1.0.0");
        
        return response;
    }
}